home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10687 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  84 lines

  1. Path: mail2news.demon.co.uk!pbutler.demon.co.uk
  2. From: Peter Hugh Butler <Peter@pbutler.demon.co.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: "Deep" Destructor Query
  5. Date: Fri, 08 Mar 96 03:53:00 GMT
  6. Organization: Myorganisation
  7. Message-ID: <826257180snz@pbutler.demon.co.uk>
  8. Reply-To: Peter@pbutler.demon.co.uk
  9. X-NNTP-Posting-Host: pbutler.demon.co.uk
  10. X-Newsreader: Demon Internet Simple News v1.30
  11. X-Mail2News-Path: pbutler.demon.co.uk
  12.  
  13. I have two questions, hope nobody minds.  Please answer by e-mail, unless
  14. you think that the group would benefit from the answer.
  15.  
  16. I have read the FAQ on this subject, but I am still a little confused.
  17. (By the way, I know I should use a dynamic multi-dimensional array, but I
  18. just wanted to try it this way).
  19.  
  20. Destructors
  21.  
  22. I have two classes, one instantiated in the other.  One (shape) contains a
  23. dynamic array of structs, the other (object) contains a dynamic array of 
  24. shapes.  Declared sort of like this:
  25.  
  26. struct point
  27. {
  28. int x, y, z;
  29. };
  30.  
  31. Class Shape
  32. {
  33. private:
  34. point *p;  //dynamic array of points
  35. //...etc
  36. };
  37.  
  38. Class Object
  39. {
  40. private:
  41. int numshapes;  //number of data elements
  42. shape *shapes;  //dynamic array of shapes
  43. //etc...
  44. };
  45.  
  46. Now, this all works quite well, apart from when it comes to destructor
  47. functions.  For shape I have:
  48.  
  49. Shape::~Shape()
  50. {
  51. if (p)
  52.         delete [] p;
  53. }
  54.  
  55. which works ok.  But when I wrote a destructor for Object, like this:
  56.  
  57. Object::~Object()
  58. {
  59. for (int i = 0; i < numshapes; i++)
  60.         shapes[i].~shape();  //"Deep" destructor, deletes underlying arrays.
  61.  
  62. delete [] shapes;  //delete the array of pointers
  63. }
  64.  
  65. The program causes a General Protection Fault (guess which OS I'm using and 
  66. win a cookie).  It fails when it tries to delete the array of null pointers
  67. pointed to by shape.  It works when I leave the "delete [] shapes;" line out.
  68.  
  69. Well, I have two questions:
  70.  
  71. 1) If I simply "delete [] shapes" with no loop to delete the arrays underneath,
  72.    will the ~shape() destructor be automatically called, thus deleting the
  73.    shape objects correctly?  I wouldn't think so, but it would be good to be
  74.    corrected.
  75.  
  76. 2) If I delete each shape with a loop (as above), does this mean that shapes
  77.    pointer points to nothing?  I would have thought that it would point to
  78.    an array of null pointers.
  79.  
  80. Thanks for any comments/opinions/advice.
  81.  
  82. -- 
  83. Peter Hugh Butler
  84.